package com.ym.b2b.news.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ym.b2b.commons.conf.AgentToken;
import com.ym.b2b.commons.conf.WebProviderTokenUser;
import com.ym.b2b.commons.conf.WebTokenUser;
import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.news.service.EmBNewsService;
import com.ym.b2b.news.sqlbean.EmBNews;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

/***
 * @ClassName:ManagerNewsController
 * @Description: 新闻处理
 * @Author:chinavane
 * @Date: 2015年1月6日 下午12:16:03
 * @Modify:2015年1月6日 下午12:16:03
 * @CopyRight 珍诚医药在线股份有限公司
 */
@Controller
@RequestMapping("/news")
public class NewsController extends BaseController {

	@Autowired
	private EmBNewsService emBNewsService;

	@RequestMapping("searchPageNews.s")
	public String searchPageNews(EmBNews emBNews, Page<EmBNews> page, HttpServletRequest req) {
		Result result = new Result();
		try {
			page.setPageable("true");
			page.setPageUrl(req);
			emBNews.setNewsStatus(1);
			result = emBNewsService.searchPageEmBNews(emBNews, page);
			req.setAttribute("result", result);
		} catch (Exception e) {
			result.setCodeResult(Result.CODE_100, e.getMessage());
		}
		return "cms/news/news_list_d";
	}

	@RequestMapping("showNewsDetail.s")
	public String showNewsDetail(EmBNews emBNews, HttpServletRequest req) {
		try {
			EmBNews result = emBNewsService.searchEmBNews(emBNews);
			WebTokenUser tokenUser = getWebTokenUser(req);
			WebProviderTokenUser providerTokenUser = getWebProviderTokenUser(req);
			AgentToken agentToken = getAgentToken(req);
			if (tokenUser == null && providerTokenUser == null && agentToken == null && result.getNewsTypeId() == 278L) {
				result.setNewsContent("促销信息登录可见");
			}
			// search中已经尝试update了
			/*
			 * if (null != result) { EmBNews emBNewsUpdate = new EmBNews();
			 * emBNewsUpdate.setNewsId(result.getNewsId());
			 * emBNewsUpdate.setViewAmount(result.getViewAmount() + 1);
			 * emBNewsService.updateEmBNews(emBNewsUpdate); }
			 */
			req.setAttribute("detailNews", result);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "cms/news/news_detail_d";
	}

	@RequestMapping("searchPromoProductNews.s")
	public String searchPromoProductNews(EmBNews emBNews, Page<EmBNews> page, HttpServletRequest req) {
		Result result = new Result();
		try {

			page.setPageable("true");
			page.setPageUrl(req);
			emBNews.setNewsStatus(1);
			SimpleDateFormat sdf   =   new   SimpleDateFormat("yyyy-MM-dd");
			emBNews.setEndDate(sdf.format(new Date()));
			result = emBNewsService.searchPageEmBNews(emBNews, page);
			req.setAttribute("result", result);
		} catch (Exception e) {
			result.setCodeResult(Result.CODE_100, e.getMessage());
		}
		return "cms/news/promo_news_list_d";
	}
}
